package com.zxq.common.utils;

import com.github.rholder.retry.*;
import com.zxq.common.utils.retry.RetryLogListener;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class RetryUtil {
    public static <T> T retry(long sleepTime, int attemptNumber, Callable<T> callable) throws ExecutionException, RetryException {
        Retryer<T> retryer = RetryerBuilder.<T>newBuilder()
                .retryIfException()
                // 重试条件
               // .retryIfResult(Objects::isNull)
                // 等待策略
                .withWaitStrategy(WaitStrategies.fixedWait(sleepTime, TimeUnit.MILLISECONDS))
                // 停止策略
                .withStopStrategy(StopStrategies.stopAfterAttempt(attemptNumber))
                // 自定义重试监听器
                .withRetryListener(new RetryLogListener())
                .build();

        return retryer.call(callable);
    }
}
